import axios from 'axios'
import app from '../../assets/js/app/index.js'
import store from '@/store'
import convertHomeJson from './convertHomeJson'
import convertIndustryCloudJson from './convertIndustryCloudJson'
import convertIndustrialMapJson from './convertIndustrialMapJson'
import convertAreaJson from './convertAreaJson'
import convertEntIndJson from './convertEntIndJson'

let homeRequest = function () {
  return axios.get('./ExcelData/home.xlsx', {
    responseType: 'arraybuffer'
  })
}
let industryCloudRequest = function () {
  return axios.get('./ExcelData/industryCloud.xlsx', {
    responseType: 'arraybuffer'
  })
}
let industrialMapRequest = function () {
  return axios.get('./ExcelData/industrialMap.xlsx', {
    responseType: 'arraybuffer'
  })
}
//获取片区
function getArea() {
  return axios.get('./ExcelData/homeParams.xlsx', {
    responseType: 'arraybuffer'
  })
}

//获取产业云图筛选条件 enterprise_scale 、 industry_type
function getEntInd() {
  return axios.get('./ExcelData/cloudParams.xlsx', {
    responseType: 'arraybuffer'
  })
}

const init = () => {
  // 分路由页面 进行数据分别调取
  if (app.control.to.path == '/home') {
    getArea()
      .then((res) => {
        let areaJson = app.dataConversion.xlsx2json(new Uint8Array(res.data), {
          type: 'array'
        })
        let area = convertAreaJson(areaJson)
        store.commit('setArea', area)
      })
      .catch(() => {})
    homeRequest()
      .then((res) => {
        let homeJson = app.dataConversion.xlsx2json(new Uint8Array(res.data), {
          type: 'array'
        })
        let home = convertHomeJson(homeJson)
        app.$emit(app.EventType.dataInit, { data: home, force: true })
      })
      .catch(() => {})
  } else if (app.control.to.path == '/industryCloud') {
    getEntInd()
      .then((res) => {
        let EntIndJson = app.dataConversion.xlsx2json(
          new Uint8Array(res.data),
          { type: 'array' }
        )
        let EntInd = JSON.parse(JSON.stringify(convertEntIndJson(EntIndJson)))
        store.commit('setEntInd', EntInd)
      })
      .catch(() => {})
    industryCloudRequest()
      .then((res) => {
        let industryCloudJson = app.dataConversion.xlsx2json(
          new Uint8Array(res.data),
          { type: 'array' }
        )
        let industryCloud = convertIndustryCloudJson(industryCloudJson)
        app.$emit(app.EventType.dataInit, { data: industryCloud, force: true })
      })
      .catch(() => {})
  } else if (app.control.to.path == '/industrialMap') {
    industrialMapRequest()
      .then((res) => {
        let industrialMapJson = app.dataConversion.xlsx2json(
          new Uint8Array(res.data),
          { type: 'array' }
        )
        let industrialMap = convertIndustrialMapJson(industrialMapJson)
        app.$emit(app.EventType.dataInit, { data: industrialMap, force: true })
      })
      .catch(() => {})
  }
}
export { init }
